package com.cdp.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdp.common.core.domain.entity.*;
import com.cdp.system.dto.DurabilityPerformanceDTO;
import com.cdp.system.mapper.SysDeformationPerformanceMapper;
import com.cdp.system.mapper.SysDurabilityPerformanceMapper;
import com.cdp.system.service.*;
import com.cdp.system.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysDurabilityPerformanceServiceImpl extends ServiceImpl<SysDurabilityPerformanceMapper, SysDurabilityPerformance> implements ISysDurabilityPerformanceService {

    @Autowired
    private SysDeformationPerformanceMapper sysDeformationPerformanceMapper;
    @Autowired
    private  SysDurabilityPerformanceMapper sysDurabilityPerformanceMapper;
    @Autowired
    private ISysCementService sysCementService;

    @Autowired
    private ISysStoneService sysStoneService;

    @Autowired
    private ISysWaterReducerService sysWaterReducerService;

    @Autowired
    private ISysStoneSizeService stoneSizeService;

    @Autowired
    private ISysSourceNameService sysSourceNameService;
    @Autowired
    private ISysEfficiencyMerchantNameService sysEfficiencyMerchantNameService;


    @Autowired
    private ISysDeformationPerformanceService sysDeformationPerformanceService;

    @Autowired
    private ISysEfficiencyAgentService sysEfficiencyAgent;

    @Autowired
    private ISysDurabilityPerformanceService sysDurabilityPerformanceService;

    @Autowired
    private ISysWaterService waterService;

    @Autowired
    private ISysMineralPowderService sysMineralPowderService;

    @Autowired
    private SysOtherAdmixturesService sysOtherAdmixturesService;


    @Autowired
    private ISysWaterModelService sysWaterModelService;

    @Autowired
    private ISysSwellingagentService sysSwellingagentService;

    @Autowired
    private ISysFlyashService sysFlyashService;



    @Autowired
    private ISysSandService sysSandService;

    /**
     * 查询耐久信息
     *
     * @return
     */
    @Override
    public List<SysDurabilityPerformance> listDurabilityPer() {
        List<SysDurabilityPerformance> list = list();
        return list;
    }


    @Override
    public Map<String, Object> queryDurabilityPerformanceaxMin() {
        Map<String, Object> result =  sysDurabilityPerformanceMapper.queryDurabilityPerformanceaxMin();
        return result;
    }

    /**
     *耐力性能模糊查询
     * @param durabilityPerformanceDTO
     * @return
     */


    @Override
    public List<DurabilityPerformanceVO> queryMistedDeformationPerformance(DurabilityPerformanceDTO durabilityPerformanceDTO) {

        // 调用 Mapper 层查询数据
        List<DurabilityPerformanceVO> results = sysDurabilityPerformanceMapper.queryMistedDeformationPerformance(durabilityPerformanceDTO);


        // 预加载相关数据，减少循环中查询
        Map<Long, SysWater> waterMap = waterService.list()
                .stream()
                .collect(Collectors.toMap(
                        sysWater -> sysWater.getWaterId().longValue(),
                        item -> item
                ));



        Map<Long, SysCement> cementMap =  sysCementService.list()
                .stream()
                .collect(Collectors.toMap(
                        sysCement -> sysCement.getCementId().longValue(),
                        item -> item
                ));


        Map<Long, SysStone> stoneMap = sysStoneService.list()
                .stream()
                .collect(Collectors.toMap(
                        sysStone -> sysStone.getStoneId().longValue(),
                        item -> item
                ));

        Map<Long, SysSand> sandMap = sysSandService.list()
                .stream()
                .collect(Collectors.toMap(
                        sysSand -> sysSand.getSandId().longValue(),
                        item -> item
                ));

        Map<Long, SysFlyash> flyashMap = sysFlyashService.list()
                .stream()
                .collect(Collectors.toMap(
                        sysFlyash -> sysFlyash.getFlyashId().longValue(),
                        item -> item
                ));

        Map<Long, SysWaterReducer> reducerMap = sysWaterReducerService.list()
                .stream()
                .collect(Collectors.toMap(
                        sysWaterReducer -> sysWaterReducer.getReducerId().longValue(),
                        item -> item
                ));

        Map<Long, SysEfficiencyAgent> efficiencyAgentMap = sysEfficiencyAgent.list()
                .stream()
                .collect(Collectors.toMap(
                        sysEfficiencyAgent -> sysEfficiencyAgent.getAgentId().longValue(),
                        item -> item
                ));

        Map<Long, SysMineralPowder> mineralPowderMap = sysMineralPowderService.list()
                .stream()
                .collect(Collectors.toMap(
                        sysMineralPowder -> sysMineralPowder.getPowderId().longValue(),
                        item -> item
                ));

        Map<Long, SysOtherAdmixtures> otherAdmixturesMap = sysOtherAdmixturesService.list()
                .stream()
                .collect(Collectors.toMap(
                        sysOtherAdmixtures -> sysOtherAdmixtures.getAdmixturesId().longValue(),
                        item -> item
                ));


        Map<Long, SysSwellingagent> swellingagentMap = sysSwellingagentService.list()
                .stream()
                .collect(Collectors.toMap(
                        sysSwellingagent -> sysSwellingagent.getSwellingagentId().longValue(),
                        item -> item
                ));

        // 处理结果集
        List<DurabilityPerformanceVO> processedResults = results.stream()
                .map(entity -> {
                    DurabilityPerformanceVO vo = new DurabilityPerformanceVO();
                    // 使用 BeanUtils 复制基础属性
                    BeanUtils.copyProperties(entity, vo);



                    // 设置水数据
                    if (entity.getWaterId() != null) {
                        SysWater sysWater = waterMap.get(Long.valueOf(entity.getWaterId()));
                        if (sysWater != null) {
                            SysWaterVO sysWaterVO = new SysWaterVO();
                            BeanUtils.copyProperties(sysWater, sysWaterVO);
                            vo.setWaterModel(sysWater.getModel());
                            vo.setSysWaterVO(sysWaterVO);
                        }
                    }

                    // 设置水泥数据
                    if (entity.getCementId() != null) {
                        SysCement sysCement = cementMap.get(Long.valueOf(entity.getCementId()));
                        if (sysCement != null) {
                            SysCementVO sysCementVO = new SysCementVO();
                            BeanUtils.copyProperties(sysCement, sysCementVO);
                            vo.setSysCementVO(sysCementVO);
                            vo.setCementModel(sysCementVO.getModel());
                        }
                    }

                    // 设置石头数据
                    if (entity.getStoneId() != null) {
                        SysStone sysStone = stoneMap.get(Long.valueOf(entity.getStoneId()));
                        if (sysStone != null) {
                            SysStoneVO sysStoneVO = new SysStoneVO();
                            BeanUtils.copyProperties(sysStone, sysStoneVO);
                            vo.setSysStoneVO(sysStoneVO);
                            vo.setStoneModel(sysStoneVO.getModel());

                        }
                    }

                    // 设置砂数据
                    if (entity.getSandId() != null) {
                        SysSand sysSand = sandMap.get(Long.valueOf(entity.getSandId()));
                        if (sysSand != null) {
                            SysSandVO sysSandVO = new SysSandVO();
                            BeanUtils.copyProperties(sysSand, sysSandVO);
                            vo.setSysSandVO(sysSandVO);
                            vo.setSandModel(sysSandVO.getType());
                        }
                    }

                    // 设置粉煤灰数据
                    if (entity.getFlyAshId() != null) {
                        // SysFlyash sysFlyash = flyashMap.get(Long.parseLong(entity.getFlyAshId());
                        long flyAshId = entity.getFlyAshId().longValue();  // 将 Integer 转换为 long
                        SysFlyash sysFlyash = flyashMap.get(flyAshId);  // 使用 long 类型的键来查找


                        if (sysFlyash != null) {
                            SysFlyashVO sysFlyashVO = new SysFlyashVO();
                            BeanUtils.copyProperties(sysFlyash, sysFlyashVO);
                            vo.setSysFlyashVO(sysFlyashVO);
                            vo.setFlyModel(sysFlyashVO.getGrade());
                        }
                    }

                    // 设置减水剂数据
                    if (entity.getReducerId() != null) {
                        SysWaterReducer sysWaterReducer = reducerMap.get(Long.valueOf(entity.getReducerId()));
                        if (sysWaterReducer != null) {
                            AdditiveWaterReducerVO waterReducerVO = new AdditiveWaterReducerVO();
                            BeanUtils.copyProperties(sysWaterReducer, waterReducerVO);
                            vo.setAdditiveWaterReducerVO(waterReducerVO);
                            vo.setWaterReducerModel(waterReducerVO.getModel());

                        }
                    }

                    // 设置增效剂数据
                    if (entity.getAgentId() != null) {
                        SysEfficiencyAgent sysEfficiencyAgent = efficiencyAgentMap.get(Long.valueOf(entity.getAgentId()));
                        if (sysEfficiencyAgent != null) {
                            SysEfficiencyAgentVO sysEfficiencyAgentVO = new SysEfficiencyAgentVO();
                            BeanUtils.copyProperties(sysEfficiencyAgent, sysEfficiencyAgentVO);
                            vo.setSysEfficiencyAgentVO(sysEfficiencyAgentVO);
                            vo.setAgentModel(sysEfficiencyAgent.getModel());

                        }
                    }

                    // 设置矿粉数据
                    if (entity.getPowderId() != null) {
                        SysMineralPowder sysMineralPowder = mineralPowderMap.get(Long.valueOf(Long.valueOf(entity.getPowderId())));
                        if (sysMineralPowder != null) {
                            SysMineralPowderVO sysMineralPowderVO = new SysMineralPowderVO();
                            BeanUtils.copyProperties(sysMineralPowder, sysMineralPowderVO);
                            vo.setMineralPowderVO(sysMineralPowderVO);
                            vo.setMineralPowderModel(sysMineralPowderVO.getModel());
                        }
                    }

                    // 设置其他数据
                    if (entity.getAdmixturesId() != null) {
                        // 将 String 类型的 admixturesId 转换为 long 类型
                        String admixturesIdStr = entity.getAdmixturesId();
                        long admixturesId = Long.parseLong(admixturesIdStr);  // 将字符串转换为 long
                        // 修改为从 otherAdmixturesMap 获取 SysOtherAdmixtures 对象
                        SysOtherAdmixtures sysOtherAdmixtures = otherAdmixturesMap.get(admixturesId);  // 使用 long 类型的键来查找

                        System.out.println("Checking flyashMap for flyAshId哈哈哈: " + sysOtherAdmixtures);
                        System.out.println("flyashMap:哈哈哈 你好" + admixturesId);  // 打印整个 map 内容
                        if (sysOtherAdmixtures != null) {
                            SysOtherAdmixturesVO sysOtherAdmixturesVO = new SysOtherAdmixturesVO();
                            BeanUtils.copyProperties(sysOtherAdmixtures, sysOtherAdmixturesVO);
                            vo.setSysOtherAdmixturesVO(sysOtherAdmixturesVO);
                            vo.setOtherAdmixturesModel(sysOtherAdmixturesVO.getModel());
                        }
                    }

                    // 设置膨胀剂数据
                    if (entity.getSwellingagentId() != null) {
                        SysSwellingagent sysSwellingagent = swellingagentMap.get(entity.getSwellingagentId().longValue());
                        if (sysSwellingagent != null) {
                            SwellingagentVO swellingagentVO = new SwellingagentVO();
                            BeanUtils.copyProperties(sysSwellingagent, swellingagentVO);
                            vo.setSwellingagentVO(swellingagentVO);
                            vo.setSwellingagentModel(swellingagentVO.getModel());
                        }
                    }



                    return vo;
                })
                .collect(Collectors.toList());

        // 返回处理后的结果
        return processedResults;
    }



    @Override
    public List<SysDurabilityPerformance> exportDurabilityPerformanceExcelText(List<Integer> durabilityIds) {


        if (durabilityIds == null || durabilityIds.isEmpty()) {
            return Collections.emptyList();
        }


        List<SysDurabilityPerformance> sysDeformationPerformanceList=  sysDurabilityPerformanceMapper.exportDurabilityPerformanceExcelText(durabilityIds);

        return sysDeformationPerformanceList;

    }

}
